package br.com.primeira.activities;

import org.andengine.engine.Engine;
import org.andengine.engine.camera.ZoomCamera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.util.FPSLogger;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.ui.activity.BaseGameActivity;

import br.com.primeira.scenes.gamescenes.MainMenuScene;
import br.com.primeira.scenes.gamescenes.MapScene;
import br.com.primeira.scenes.gamescenes.SplashScene;
import br.com.primeira.util.AssetPool;

import android.view.KeyEvent;

public class GameActivity extends BaseGameActivity
{
	public enum SceneType
	{
		SPLASH,
	    MENU,
	    OPTIONS,
	    GAME
	}
	
	// ===========================================================
	// Constants
	// ===========================================================

	private static final int CAMERA_WIDTH = 480;
	private static final int CAMERA_HEIGHT = 800;

	// ===========================================================
	// Fields
	// ===========================================================
	
	private SceneType currentScene;
	
	private MainMenuScene menuScene;
	private SplashScene splashScene;
	private MapScene mapScene;
	
	//private BitmapTextureAtlas splashScreenTextureAtlas;
	
	// ===========================================================
	// Constructors
	// ===========================================================

	// ===========================================================
	// Getter & Setter
	// ===========================================================

	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================
	
	@Override
	public EngineOptions onCreateEngineOptions()
	{
		final ZoomCamera camera = new ZoomCamera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		
		return new EngineOptions(true, ScreenOrientation.PORTRAIT_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
	}

	@Override
	public void onCreateResources(OnCreateResourcesCallback pOnCreateResourcesCallback)
	{
	    BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
	    
	    AssetPool assetPool = AssetPool.getInstance();
	    assetPool.init(this, mEngine);
	    
	    assetPool.loadTextureRegion("splash", "splash.png", 256, 256);
	    
		assetPool.loadMap("mapa1", "Primeira.oel");

		pOnCreateResourcesCallback.onCreateResourcesFinished();
	}

	@Override
	public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
	{
		mEngine.registerUpdateHandler(new FPSLogger());
		
		splashScene = new SplashScene(this);
		
        // -------------------------------------------
		
		menuScene = new MainMenuScene(this);
		
		// ---------------------------------------------
		
		mapScene = new MapScene(this);
		
		// ---------------------------------------------
		
		currentScene = SceneType.SPLASH;
		
        pOnCreateSceneCallback.onCreateSceneFinished(splashScene);
	}
	
	@Override
    public void onPopulateScene(final Scene pScene, OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception
    {  
		pOnPopulateSceneCallback.onPopulateSceneFinished();
    }
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN)
		{          
			switch (currentScene)
			{
				case SPLASH:
					break;
					
				case MENU:
					System.exit(0);
					break;
					
				case OPTIONS:
					currentScene = SceneType.MENU;
					mEngine.setScene(menuScene);
					break;
					
				case GAME:
					currentScene = SceneType.MENU;
					mEngine.setScene(menuScene);
					break; 
			}
		}
		 
		return false;
	}
	
	// ===========================================================
	// Methods
	// ===========================================================
	
	public Engine getEngine()
	{
		return mEngine;
	}
	
	public void setScene(SceneType sceneType)
	{
		currentScene = sceneType;
		
		switch (sceneType)
		{
			case SPLASH:
				mEngine.setScene(splashScene);
				break;
				
			case MENU:
				mEngine.setScene(menuScene);
				break;
				
			case GAME:
				mEngine.setScene(mapScene);
				break;
		}
	}
	
	// ===========================================================
	// Inner and Anonymous Classes
	// ===========================================================
}
